package top.jamewen.framework.sbmp.controller;

import cn.hutool.core.collection.CollUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import top.jamewen.framework.sbmp.common.api.CommonResult;
import top.jamewen.framework.sbmp.common.model.UmsAdmin;
import top.jamewen.framework.sbmp.common.model.UmsRole;
import top.jamewen.framework.sbmp.model.qo.UmsAdminLoginQO;
import top.jamewen.framework.sbmp.model.qo.UmsAdminQO;
import top.jamewen.framework.sbmp.service.UmsAdminService;
import top.jamewen.framework.sbmp.service.UmsMenuService;
import top.jamewen.framework.sbmp.service.UmsRoleService;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 后台登录注册模块
 * @author JameWen
 * @version V1.0
 * @description
 * @Date 2021/1/14
 */
@Slf4j
@RestController
@Api(tags = "后台登录注册模块")
@RequestMapping("/oss")
public class UmsOssController {
    @Value("${security.jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private UmsAdminService adminService;

    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public CommonResult<String> register(@Validated @RequestBody UmsAdminQO adminQo) {
        try{
            return CommonResult.success(adminService.register(adminQo));
        }catch (Exception e){
            log.error("注册失败：{}",e.getMessage());
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation(value = "用户登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public CommonResult<Map<String, String>> login(@Validated @RequestBody UmsAdminLoginQO adminLoginQo) {
        String token = adminService.login(adminLoginQo.getUsername(), adminLoginQo.getPassword());
        if (token == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return CommonResult.success(tokenMap);
    }
}
